# -*- coding: utf-8 -*-

from odoo import api, fields, tools, models, _


class ProjectProject(models.Model):
    _inherit = "project.project"

    purchase_ids = fields.One2many('purchase.order', 'project_id', string='采购明细')
    sale_ids = fields.One2many('sale.order', 'project_id', string='销售明细')
    cost_ids = fields.One2many('project.cost', 'project_id', string='费用明细')
    total = fields.Float(string='项目总金额')
    profit = fields.Float(string='当前利润',compute="_compute_profit")

    def _compute_profit(self):
        total_profit = 0
        total_sale_money = 0
        total_purchase_money = 0
        cost_money = 0
        for i in self.sale_ids:
            total_sale_money = total_sale_money + i.amount_total
        for i in self.purchase_ids:
            total_purchase_money = total_purchase_money + i.amount_total
        for i in self.cost_ids:
            cost_money = cost_money + i.total
        self.profit = total_sale_money- total_purchase_money-cost_money